package org.kitten.core.util;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HttpUtil {
    private static final Log logger = LogFactory.getLog(HttpUtil.class);
	public static boolean isContentMgmtServerRunning(String wsdl) {
		int connectTimeout = 3000;
		if (!wsdl.endsWith("?wsdl")) {
			wsdl += "?wsdl";
		}
		HttpMethod method = new HeadMethod(wsdl);
		HttpClient client = new HttpClient();
		// 设置代理服务器地址和端口
		// 由于要上传的文件可能比较大,因此在此设置最大的连接超时时间
		client.getHttpConnectionManager().getParams().setConnectionTimeout(
				connectTimeout);
		client.getHttpConnectionManager().getParams().setSoTimeout(
				connectTimeout);
		System.setProperty("sun.net.client.defaultReadTimeout", String
				.valueOf(connectTimeout));
		try {
			client.executeMethod(method);
		} catch (ConnectTimeoutException e) {
			logger.warn("connect timeout,more than " + connectTimeout);
			return false;
		} catch (Exception e) {
			logger.warn("connect fail,throws exception:" + e.getMessage());
			return false;
		}
		if (method == null) {
			logger.warn("connect failed=>" + wsdl);
			return false;
		} else {
			// 打印服务器返回的状态
			if (method.getStatusCode() == HttpStatus.SC_OK) {
				logger.info("connect success2");
				method.releaseConnection();
				return true;
			} else {
				logger.warn("connect fail[statas code]:" + method.getStatusCode());
			}
			// 释放连接
			method.releaseConnection();
		}
		return false;
	}

	public static void downloadFromUrl(String url, String dest)
			throws Exception {
		int connectTimeout = 10000;//10s
		int readTimeout = 30000;//30s
		InputStream is = null;
		FileOutputStream fos = null;
		try {
			HttpMethod method = new GetMethod(url);
			HttpClient client = new HttpClient();
			client.getHttpConnectionManager().getParams().setConnectionTimeout(
					connectTimeout);
			client.getHttpConnectionManager().getParams().setSoTimeout(
					readTimeout);
			int statusCode = client.executeMethod(method);
			if (statusCode != -1) {
				is = method.getResponseBodyAsStream();
				fos = new FileOutputStream(dest);
				FileUtil.tranfer(is, fos, 1024 * 5);
				method.releaseConnection();
			} else {
				logger.warn("download url fail==>" + url);
				throw new Exception("download url error==>" + url);
			}
		} catch (Exception e) {
			logger.error(ErrorUtil.getError(e));
			throw e;
		} finally {
			FileUtil.close(fos);
			FileUtil.close(is);
		}
	}
	public static void main(String[] args) throws Exception {
		System.out.println(new Date());
		HttpUtil.downloadFromUrl("http://128.96.96.33:7001/httpfiletrans/HttpFileTrans?method=download&FilePath=/20100706185631/333/00000003.JPG", "c:/test3.JPG");
		System.out.println(new Date());
	}
}
